package com.fy.demo;


import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.util.Calendar;
import java.util.Date;

/**
 * @author hxd
 * @version 1.0
 * @ClassName JWTDemo
 * @date 2023/6/15 19:05
 * @since 1.0
 */
public class JWTDemo {
    /*public static void main(String[] args) {
        Calendar instance = Calendar.getInstance();
        instance.setTime(new Date());
        instance.add(Calendar.MINUTE,5);

        //颁发jwt令牌
        String token = JWT.create()
                //指定头部（可默认）
                //withHeader
                //指定payload
                .withClaim("id", 1)
                .withClaim("name", "张三")
                //指定过期时间
                .withExpiresAt(instance.getTime())
                //签名
                .sign(Algorithm.HMAC256("qf"));

        System.out.println(token);
    }*/

    //校验令牌
    public static void main(String[] args) {
        JWTVerifier jwtVerifier = JWT.require(Algorithm.HMAC256("qf")).build();
        DecodedJWT verify = jwtVerifier.verify("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJuYW1lIjoi5byg5LiJIiwiaWQiOjEsImV4cCI6MTY4NjgzMDYyNH0.Tr-cL7jWPlPyuhy9kxPuAh_wWCf_qqjCzahm1hwGD7E");
        Integer id = verify.getClaim("id").asInt();
        System.out.println("id = " + id);
        String name = verify.getClaim("name").asString();
        System.out.println("name = " + name);


    }
}
